package com.ncp.model.biz.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.order.OrderBasic;
import com.ncp.model.entity.order.OrderDetail;

public class ManageOrderBiz {
DB common = new DB();
	
	/*//根据用户ID取得该用户的所有订单的数据
	public OrderBasicBean getOrderInfoByOrderID(String orderID){
		OrderBasicBean orderBasicBean =new OrderBasicBean();
		String sql = "select * from ORDERBASIC where OrderID = ?";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		list.add(0, orderID);
		common.setList(list);
		Result rs = common.executeQuery();
		for(Map<String,Object> object: rs.getRows()){
			orderBasicBean.setOrderID(orderID);
			orderBasicBean.setUserID(Integer.parseInt(object.get("USERID").toString()));
			orderBasicBean.setOrderTime(object.get("ORDERTIME").toString());
			orderBasicBean.setPaymentType(object.get("PAYMENTTYPE").toString());
			orderBasicBean.setTotalPrice(Float.parseFloat(object.get("TOTALPRICE").toString()));
			orderBasicBean.setOrderStatus(object.get("ORDERSTATUS").toString());
			orderBasicBean.setReceiverName(object.get("RECEIVERNAME").toString());
			orderBasicBean.setReceiverAddress(object.get("RECEIVERADDRESS").toString());
			orderBasicBean.setReceiverZip(object.get("RECEIVERZIP").toString());
			orderBasicBean.setReceiverPhone(object.get("RECEIVERPHONE").toString());
			orderBasicBean.setReceiverMobile(object.get("RECEIVERMOBILE").toString());
			orderBasicBean.setReceiverEMail(object.get("RECEIVEREMAIL").toString());
			orderBasicBean.setLogisticsType(object.get("LOGISTICSTYPE").toString());
			orderBasicBean.setLogisticsPayment(object.get("LOGISTICSPAYMENT").toString());
			orderBasicBean.setTradeID(object.get("TRADEID").toString());
			orderBasicBean.setBuyerAccount(object.get("BUYERACCOUNT").toString());
			orderBasicBean.setConsignmentTime(object.get("CONSIGNMENTTIME").toString());
			orderBasicBean.setExpressID(object.get("EXPRESSID").toString());
		}
		return orderBasicBean;
	}*/
	
	/**
	 * 取得用户所有订单
	 * @param userID
	 */
	public List<OrderBasic> getOrderBasics(int userID){
		List<OrderBasic> orders = new ArrayList<OrderBasic>();
		String sql = "select orderid, ordertime, paymenttype, totalprice, orderstatus, logisticstype, expressID, receiverID from ORDERBASIC where userid = " + userID;
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		Result rs = common.executeQuery();
		if(rs.getRowCount() <= 0){
			return null;
		}
		for(Map o : rs.getRows()){
			OrderBasic order = new OrderBasic();
			
			order.setOrderID(o.get("ORDERID").toString());
			order.setOrderTime(o.get("ORDERTIME").toString());
			order.setPaymentType(o.get("PAYMENTTYPE").toString());
			order.setTotalPrice(Float.parseFloat(o.get("TOTALPRICE").toString()));
			order.setOrderStatus(o.get("ORDERSTATUS").toString());
			order.setLogisticsType(o.get("logisticstype").toString());
			order.setExpressID(o.get("expressID").toString());
			order.setReceiverID(Integer.parseInt(o.get("receiverID").toString()));
			
			//System.out.println("order ID : " + o.get("ORDERID").toString());
			
			//取订单中的 产品
			String sql2 = "select productID, productPrice, productNumber from ORDERDETAIL where orderID = '"+ o.get("orderID").toString() +"'";
			common.setSql(sql2);
			common.setConn(DbConnection.getConnByDataSource());
			Result rs2 = common.executeQuery();
			order.setDetails(new ArrayList<OrderDetail>());
			for(Map v : rs2.getRows()){
				OrderDetail detail = new OrderDetail();
				
				//System.out.println("product ID : " + v.get("productID").toString());
				detail.setProductID(Integer.parseInt(v.get("productID").toString()));
				detail.setProductPrice(Float.parseFloat(v.get("productPrice").toString()));
				detail.setProductNumber(Integer.parseInt(v.get("ProductNumber").toString()));
				
				order.getDetails().add(detail);
			}
			orders.add(order);
		}
		return orders;
	}
	
	/**
	 * 根据订单号取得订单
	 * @param orderID
	 */
	public OrderBasic getOrderByID(String orderID){
		String sql = "select orderid, ordertime, paymenttype, totalprice, orderstatus, logisticstype, expressID, receiverID from ORDERBASIC where orderID = " + orderID;
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		Result rs = common.executeQuery();
		if(rs.getRowCount() <= 0){
			return null;
		}
		OrderBasic order = new OrderBasic();
		for(Map o : rs.getRows()){
						
			order.setOrderID(o.get("ORDERID").toString());
			order.setOrderTime(o.get("ORDERTIME").toString());
			order.setPaymentType(o.get("PAYMENTTYPE").toString());
			order.setTotalPrice(Float.parseFloat(o.get("TOTALPRICE").toString()));
			order.setOrderStatus(o.get("ORDERSTATUS").toString());
			order.setLogisticsType(o.get("logisticstype").toString());
			order.setExpressID(o.get("expressID").toString());
			order.setReceiverID(Integer.parseInt(o.get("receiverID").toString()));			
			
			//取订单中的 产品
			String sql2 = "select productID, productPrice, productNumber from ORDERDETAIL where orderID = '"+ o.get("orderID").toString() +"'";
			common.setSql(sql2);
			common.setConn(DbConnection.getConnByDataSource());
			Result rs2 = common.executeQuery();
			order.setDetails(new ArrayList<OrderDetail>());
			for(Map v : rs2.getRows()){
				OrderDetail detail = new OrderDetail();

				detail.setProductID(Integer.parseInt(v.get("productID").toString()));
				detail.setProductPrice(Float.parseFloat(v.get("productPrice").toString()));
				detail.setProductNumber(Integer.parseInt(v.get("ProductNumber").toString()));
				
				order.getDetails().add(detail);
			}			
		}
		return order;
		
	}
	
	/**
	 * 取得用户不同状态的订单数
	 * @param status
	 * @param userID
	 */
	public int getOrderNumByStatus(int userID, String status){
		int result = 0;
		String sql = "select count(*) orderNumber from ORDERBASIC where userid = " + userID + " and orderstatus = '"+ status +"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		Result rs = common.executeQuery();
		for(Map o : rs.getRows()){
			result = Integer.parseInt(o.get("orderNumber").toString());
		}
		return result;
	}
	
	/**
	 * 取消订单/删除订单，根据订单号
	 * @param orderID
	 */
	public int cancelOrder(String orderID){
		int result = 0;
		String sql = "delete from ORDERBASIC where orderID = '"+ orderID +"'";
		common.setSql(sql);
		common.setConn(DbConnection.getConnByDataSource());
		result = common.executeUpdate();
		return result;
	}
	
}
